package com.mybatis.lo.sqlsession;

import com.mybatis.lo.config.Configuration;
import com.mybatis.lo.config.MappedStatement;
import com.mybatis.lo.executor.Executor;
import lombok.AllArgsConstructor;

import java.util.List;

/**
 * 内置的`SqlSession`实现，通过注入的执行器进行查询
 * @author zhangbh
 * 2023-06-16 08:21
 **/
@AllArgsConstructor
public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;
    /**
     * 执行器
     */
    private Executor executor;


    @Override
    public <T> List<T> queryList(String statementId, Object param) {
        MappedStatement mappedStatement = configuration.getMappedStatement(statementId);
        return executor.queryList(mappedStatement, configuration, param);
    }

    @Override
    public <T> T queryOne(String statementId, Object param) {
        List<T> list = queryList(statementId, param);
        return list == null || list.isEmpty() ? null : list.get(0);
    }
}
